//
//  FMDBManage.m
//  ITHome
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "FMDBManage.h"
#import "UserInfoModel.h"
#import "RecentActivityModel.h"
#import "TVViewModel.h"
#import "VideoModel.h"
#import "JokeModel.h"

// 数据库名
#define kITHomeDB @"ITDB"
#define kMyTableName @"myCollectedInfo" // 收藏信息表
#define kUserInfoTable @"UserIndoTable" // 用户表
#define kCacheTabel @"CacheTable"       //缓存公用表
#define kTVViewTable @"TVViewTable"       //视频资讯 离线表
#define kVideoTable @"VideoTable"            //娱乐视频表
#define kJokeTable @"JokeTabel"            //娱乐段子表


@implementation FMDBManage

#pragma mark - 收藏模块
// 获取文件路径
+ (NSString *)documentsPath:(NSString *)dbName
{
    // 拼接一个.db
    NSString *nameA = [NSString stringWithFormat:@"%@.db",dbName];
    
//    YAXLog(@"%@",[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:nameA]);
    return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:nameA];
}

// 建表
+ (void)creatTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    if ([db open])
    {
        NSString *sqlCreatTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (ID integer primary key autoincrement,urlId text not null unique,shareTitle text,created_at text,urlString text,commentsCount text,shareImageUrl text)",kMyTableName];
        [db executeUpdate:sqlCreatTable];

        [db close];
    }
    db = nil;
}

// 收藏一条信息
+ (void)insertOneInfo:(NSString *)urlId shareTitle:(NSString *)shareTitle created_at:(NSString *)created_at urlString:(NSString *)urlString commentsCount:(NSString *)commentsCount shareImageUrl:(NSString *)shareImageUrl
{
    if (!urlId) {
        return;
    }
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(urlId,shareTitle,created_at,urlString,commentsCount,shareImageUrl) values(?,?,?,?,?,?)",kMyTableName];
        [db executeUpdate:sql,urlId,shareTitle,created_at,urlString,commentsCount,shareImageUrl];

        [db close];
    }
    db = nil;
}

// 根据urlID删除一条收藏
+ (void)deleteOneInfoWithUrlId:(NSString *)urlId
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@ where urlID = ?",kMyTableName];
        [db executeUpdate:sql,urlId];

        [db close];
    }
    db = nil;
}

// 根据urlID获得一条收藏
+ (FMDBManageModel *)getOneInfoWithUrlID:(NSString *)urlID
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    FMDBManageModel *model = [FMDBManageModel new];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@ where urlID = ?",kMyTableName];
        FMResultSet *rs = [db executeQuery:sql,urlID];
        while ([rs next])
        {
            model.urlId = [rs stringForColumn:@"urlID"];
            model.shareTitle = [rs stringForColumn:@"shareTitle"];
            model.created_at = [rs stringForColumn:@"created_at"];
            model.urlString = [rs stringForColumn:@"urlString"];
            model.commentsCount = [rs stringForColumn:@"commentsCount"];
            model.feature_img = [rs stringForColumn:@"shareImageUrl"];
        }
        [db close];
    }
    db = nil;
    return model;
}

// 获得所有收藏
+ (NSMutableArray *)getAllInfo
{
    NSMutableArray *array = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",kMyTableName];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            FMDBManageModel *model = [FMDBManageModel new];
            model.urlId = [rs stringForColumn:@"urlID"];
            model.shareTitle = [rs stringForColumn:@"shareTitle"];
            model.created_at = [rs stringForColumn:@"created_at"];
            model.urlString = [rs stringForColumn:@"urlString"];
            model.commentsCount = [rs stringForColumn:@"commentsCount"];
            model.feature_img = [rs stringForColumn:@"shareImageUrl"];
            [array addObject:model];
            model = nil;
        }
        [db close];
    }
    db = nil;
    return array;
}

// 删除所有
+ (void)deleteAllInfo
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@",kMyTableName];
        [db executeUpdate:sql];

        [db close];
    }
    db = nil;
}

// 获取所有urlId
+ (NSMutableArray *)showAllUrlID
{
    NSMutableArray *array = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",kMyTableName];
        // 从数据库取出数据
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString *urlID = [rs stringForColumn:@"urlID"];
            [array addObject:urlID];
        }
        [db close];
    }
    db = nil;
    return array;
}



//*****************************用户信息***************************//
#pragma mark - 用户信息
// 创建用户信息表
+ (void)createUserInfoTable
{
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];

    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    // 判断数据库是否已经开打
    if (![db open])
    {
//        YAXLog(@"数据库打开失败");
        return;
    }
    
    // 判断表是否存在 存在则不创建表
    if (![db tableExists:kUserInfoTable])
    {
        NSString *userInfoSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(ID integer primary key autoincrement, openId text not null, city text, figureurl_qq_1 blob, figureurl_qq_2 blob, gender text, province text, nickname text, indirect text)", kUserInfoTable];
        BOOL flag = [db executeUpdate:userInfoSql];
        if (flag)
        {
            YAXLog(@"创建表成功");
            YAXLog(@"%@",[self documentsPath:@"ITDB"]);
        }
        else
        {
            YAXLog(@"创建表失败");
        }
        [db close];
    }
    db = nil;
}

// 插入用户信息
+ (BOOL)insertUserInfoOpenId:(NSString *)openId city:(NSString *)city figureurl_qq_1:(UIImage *)figureurl_qq_1 figureurl_qq_2:(UIImage *)figureurl_qq_2 gender:(NSString *)gender province:(NSString *)province nickname:(NSString *)nickname indirect:(NSString *)indirect
{
    BOOL flag = NO;
    
    if (!openId) {
        return flag;
    }
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSData *figureurl_qq_40 = UIImageJPEGRepresentation(figureurl_qq_1, 0.5);
        NSData *figureurl_qq_100 = UIImageJPEGRepresentation(figureurl_qq_2, 0.5);
        NSString *sql = [NSString stringWithFormat:@"insert into %@(openId, city, figureurl_qq_1, figureurl_qq_2, gender, province, nickname, indirect) values(?,?,?,?,?,?,?,?)",kUserInfoTable];
        BOOL b = [db executeUpdate:sql,openId, city, figureurl_qq_40, figureurl_qq_100, gender, province, nickname, indirect];
        if (b)
        {
            flag = YES;
        }
        else
        {
            flag = NO;
        }
        [db close];
    }
    db = nil;
    return flag;
}

// 查询用户信息
+ (NSMutableArray *)selectAllUserInfo
{
    NSMutableArray *userInfoArray = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",kUserInfoTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            UserInfoModel *userInfoModel = [UserInfoModel new];
            userInfoModel.openId = [rs stringForColumn:@"openId"];
            userInfoModel.city = [rs stringForColumn:@"city"];
            
            NSData *figureurl_qq_40 = [rs dataForColumn:@"figureurl_qq_1"];
            userInfoModel.figureurl_qq_1 = [UIImage imageWithData:figureurl_qq_40];
            
            NSData *figureurl_qq_100 = [rs dataForColumn:@"figureurl_qq_2"];
            userInfoModel.figureurl_qq_2 = [UIImage imageWithData:figureurl_qq_100];
            
            userInfoModel.province = [rs stringForColumn:@"province"];
            userInfoModel.gender = [rs stringForColumn:@"gender"];
            userInfoModel.nickname = [rs stringForColumn:@"nickname"];
            userInfoModel.indirect = [rs stringForColumn:@"indirect"];
            [userInfoArray addObject:userInfoModel];
            userInfoModel = nil;
        }
        [db close];
    }
    db = nil;
    return userInfoArray;

}

// 删除所有用户信息
+ (void)deleteAllUserInfo
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@",kUserInfoTable];
        [db executeUpdate:sql];
//        if (b)
//        {
////            YAXLog(@"删除成功");
//        }
//        else
//        {
////            YAXLog(@"删除失败");
//        }
        [db close];
    }
    db = nil;
}

// 判断根据openId修改数据
+ (BOOL)updateUserInfoWithOpenId:(NSString *)openId city:(NSString *)city province:(NSString *)province figureurl_qq_2:(UIImage *)figureurl_qq_2 nickname:(NSString *)nickname indirect:(NSString *)indirect
{
    BOOL flag = NO;
    
    if (!openId) {
        return flag;
    }
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        // 先查询
        NSString *sql = [NSString stringWithFormat:@"select *from %@ where openId = ?",kUserInfoTable];
        FMResultSet *rs = [db executeQuery:sql,openId];
        if ([rs next])
        {
            NSData *figureurl_qq_100 = UIImageJPEGRepresentation(figureurl_qq_2, 0.5);
            NSString *updateSql = [NSString stringWithFormat:@"update %@ set city = ?, province = ?, figureurl_qq_2 = ?, nickname = ?, indirect = ?",kUserInfoTable];
            
            BOOL b =  [db executeUpdate:updateSql, city, province, figureurl_qq_100, nickname, indirect];
            
            // 盘面的是否修改成功
            if (b)
            {
                flag = YES;
//                YAXLog(@"修改数据成功");
            }
            else
            {
                flag = NO;
//                YAXLog(@"修改数据失败");
            }
            [db close];
        }
    }
    db = nil;
    return flag;
}


//****************************本地化数据库表**************************//
#pragma mark - 公用模块
// 创建表
+ (void)createDatabaseTabel:(NSString *)tabelName
{
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    // 判断数据库是否已经开打
    if (![db open])
    {
//        YAXLog(@"数据库打开失败");
        return;
    }
    
    // 判断表是否存在 存在则不创建表
    if (![db tableExists:tabelName])
    {
        NSString *cacheSql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(ID integer primary key autoincrement, title text, created_at text, replies_count text, featureImageUrl text)", tabelName];
        [db executeUpdate:cacheSql];
        [db close];
    }
    db = nil;
}

// 插入用户信息
+ (BOOL)insertCacheTable:(NSString *)tableName Title:(NSString *)title created_at:(NSString *)created_at replies_count:(NSString *)replies_count featureImageUrl:(NSString *)featureImageUrl
{
    BOOL flag = NO;
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(title, created_at, replies_count, featureImageUrl) values(?,?,?,?)",tableName];
        
        BOOL b = [db executeUpdate:sql,title, created_at, replies_count, featureImageUrl];
        
        if (b)
        {
            flag = YES;
        }
        else
        {
            flag = NO;
        }
        [db close];
    }
    db = nil;
    return flag;
}

// 查询用户信息
+ (NSMutableArray *)selectAllDatabaseTable:(NSString *)tableName
{
    NSMutableArray *cacheArray = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",tableName];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            ITHomePublicModel *cacheModel = [ITHomePublicModel new];
            cacheModel.title = [rs stringForColumn:@"title"];
            cacheModel.created_at = [rs stringForColumn:@"created_at"];
            cacheModel.replies_count = [rs stringForColumn:@"replies_count"];
            cacheModel.feature_img = [rs stringForColumn:@"featureImageUrl"];
          
            [cacheArray addObject:cacheModel];
            cacheModel = nil;
        }
        [db close];
    }
    db = nil;
    return cacheArray;
}

// 删除所有用户信息
+ (void)deleteAllDatabaseTable:(NSString *)tableName
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@",tableName];
        [db executeUpdate:sql];

        [db close];
    }
    db = nil;
}

//  注：查询移动互联模块的数据 因为model不同 所以写了一个单独的查询方法
+ (NSMutableArray *)selectAllRecentCacheTabel:(NSString *)tableName
{
    NSMutableArray *cacheArray = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",tableName];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            RecentActivityModel *cacheModel = [RecentActivityModel new];
            cacheModel.title = [rs stringForColumn:@"title"];
            cacheModel.lmodify = [rs stringForColumn:@"created_at"];
            cacheModel.replyCount = [rs stringForColumn:@"replies_count"].intValue;
            cacheModel.imgsrc = [rs stringForColumn:@"featureImageUrl"];
            
            [cacheArray addObject:cacheModel];
            cacheModel = nil;
        }
        [db close];
    }
    db = nil;
    return cacheArray;
}


#pragma mark - 视频资讯
// 创建表
+ (void)createTVTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    // 为数据库设置缓存,提高查询效率
    [db setShouldCacheStatements:YES];
    
    // 判断数据库是否打开
    if (![db open])
    {
        return;
    }
    
    // 判断表是否存在
    if (![db tableExists:kTVViewTable])
    {
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(ID integer primary key autoincrement, title text, videoTime text, videoImageUrl text)", kTVViewTable];
        [db executeUpdate:sql];

        [db close];
    }
    db = nil;
}

// 插入数据
+ (BOOL)insertTVTableTitle:(NSString *)title videoTime:(NSString *)videoTime videoImageUrl:(NSString *)videoImageUrl
{
    BOOL flag = NO;
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:kITHomeDB]];
    [db setShouldCacheStatements:YES];
    
    
    if ([db open])
    {
        NSString *sql = [NSString stringWithFormat:@"insert into %@(title, videoTime, videoImageUrl) values(?,?,?)",kTVViewTable];
        BOOL flag = [db executeUpdate:sql, title, videoTime, videoImageUrl];
        if (flag)
        {
            flag = YES;
        }
        else
        {
            flag = NO;
        }
        [db close];
    }
    db = nil;
    return flag;
}

// 查询数据
+ (NSMutableArray *)selectAllTVTable
{
    NSMutableArray *tvArray = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:kITHomeDB]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",kTVViewTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            TVViewModel *tvViewModel = [[TVViewModel alloc] init];
            tvViewModel.title = [rs stringForColumn:@"title"];
            tvViewModel.video_length = [rs stringForColumn:@"videoTime"];
            tvViewModel.feature_img = [rs stringForColumn:@"videoImageUrl"];
            
            [tvArray addObject:tvViewModel];
            tvViewModel = nil;
        }
        [db close];
    }
    db = nil;
    return tvArray;
}

// 删除数据
+ (void)deleteAllTVTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@",kTVViewTable];
        [db executeUpdate:sql];
        [db close];
    }
    db = nil;
}


#pragma mark - 娱乐模块中的视频模块
// 创建表
+ (void)createVideoTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    // 为数据库设置缓存,提高查询效率
    [db setShouldCacheStatements:YES];
    
    // 判断数据库是否打开
    if (![db open])
    {
        return;
    }
    
    // 判断表是否存在
    if (![db tableExists:kVideoTable])
    {
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(ID integer primary key autoincrement, title text, videoTime text, playCount text, replyCount text)", kVideoTable];
        [db executeUpdate:sql];
        [db close];
    }
    db = nil;
}

// 插入数据
+ (BOOL)insertVideoTableTitle:(NSString *)title videoTime:(NSString *)videoTime playCount:(NSString *)playCount replyCount:(NSString *)replyCount
{
    BOOL flag = NO;
    
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:kITHomeDB]];
    
    [db setShouldCacheStatements:YES];
    
    if ([db open])
    {

        NSString *sql = [NSString stringWithFormat:@"insert into %@(title, videoTime, playCount, replyCount) values(?,?,?,?,?)",kVideoTable];
        
        BOOL b = [db executeUpdate:sql, title, videoTime, replyCount];
        if (b)
        {
            flag = YES;
        }
        else
        {
            flag = NO;
        }

        [db close];
    }
    db = nil;
    return flag;
}

// 查询数据
+ (NSMutableArray *)selectAllVideoTable
{
    NSMutableArray *videoArray = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:kITHomeDB]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",kVideoTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            VideoModel *videoModel = [[VideoModel alloc] init];
            videoModel.title = [rs stringForColumn:@"title"];
            videoModel.length = [rs stringForColumn:@"videoTime"].integerValue;
            videoModel.playCount = [rs stringForColumn:@"playCount"].integerValue;
            videoModel.replyCount = [rs stringForColumn:@"replyCount"].integerValue;
            
            [videoArray addObject:videoModel];
            videoModel = nil;
        }
        [db close];
    }
    db = nil;
    return videoArray;
}

// 删除数据
+ (void)deleteAllVideoTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@",kVideoTable];
        [db executeUpdate:sql];

        [db close];
    }
    db = nil;
}

#pragma mark - 娱乐模块中的段子模块
// 创建表
+ (void)createJokeTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    // 为数据库设置缓存,提高查询效率
    [db setShouldCacheStatements:YES];
    
    // 判断数据库是否打开
    if (![db open])
    {
        return;
    }
    
    // 判断表是否存在
    if (![db tableExists:kJokeTable])
    {
        NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(ID integer primary key autoincrement, content text, login text, down text, up text)", kJokeTable];
        [db executeUpdate:sql];

        [db close];
    }
    db = nil;
}

// 插入数据
+ (BOOL)insertJokeTableContent:(NSString *)content login:(NSString *)login down:(NSString *)down up:(NSString *)up
{
    BOOL flag = NO;
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:kITHomeDB]];
    [db setShouldCacheStatements:YES];
    
    
    if ([db open])
    {
        
        NSString *sql = [NSString stringWithFormat:@"insert into %@(content, login, down, up) values(?,?,?,?)",kJokeTable];
        
        BOOL b = [db executeUpdate:sql, content, login, down, up];
        if (b)
        {
            flag = YES;
//            YAXLog(@"视频 -- 插入数据成功");
        }
        else
        {
            flag = NO;
//            YAXLog(@"视频 -- 插入数据失败");
        }
        [db close];
    }
    db = nil;
    return flag;
}

// 查询数据
+ (NSMutableArray *)selectAllJokeTable
{
    NSMutableArray *jokeArray = [NSMutableArray array];
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:kITHomeDB]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"select *from %@",kJokeTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            JokeModel *jokeModel = [[JokeModel alloc] init];
            jokeModel.content = [rs stringForColumn:@"content"];
            jokeModel.login = [rs stringForColumn:@"login"];
            jokeModel.down = [rs stringForColumn:@"down"].integerValue;
            jokeModel.up = [rs stringForColumn:@"up"].integerValue;
            
            [jokeArray addObject:jokeModel];
            jokeModel = nil;
        }
        [db close];
    }
    db = nil;
    return jokeArray;
}

// 删除数据
+ (void)deleteAllJokeTable
{
    FMDatabase *db = [FMDatabase databaseWithPath:[self documentsPath:@"ITDB"]];
    
    //为数据库设置缓存，提高查询效率
    [db setShouldCacheStatements:YES];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@",kJokeTable];
        [db executeUpdate:sql];
//        if (b)
//        {
//            YAXLog(@"删除成功");
//        }
//        else
//        {
//            YAXLog(@"删除失败");
//        }
        [db close];
    }
    db = nil;
}
@end
